# Copyright (c) Alessandro Pilotti 2009
# http://www.codeplex.com/PSOutlookManager
# info@pilotti.it

add-pssnapin OutlookAccountManagerSnapIn -ErrorAction SilentlyContinue

# Backup all the Outlook profile accounts to "$home\OutlookAccounts.xml"
#
function BackupOutlookAccounts()
{
	$a = Get-MAPIAccount "Outlook"
	$a | Export-Clixml $home\OutlookAccounts.xml
}

# Changes the SMTP server in all the non Exchange Outlook accounts.
# This is useful in cases of usage of temporary networks requiring a mandatory SMTP server.  
# Call "BackupOutlookAccounts" before this function, in order to backup the account data
# for a later restore with "RestoreOutlookAccountsSMTPServer"
#
function ChangeOutlookAccountsSMTPServer($smtpServerName)
{
	if(!$smtpServerName) {
		throw("smtpServerName parameter value is missing")
	}

	$a = Get-MAPIAccount "Outlook" | where-object {!$_.IsExchange}
	$a | % {
		$_.OutgoingServer = $smtpServerName; 
		$_.OutgoingPort = 25; 
		$_.OutgoingUseAuthentication = $false; 
		$_.OutgoingSSL = $false; 
		$_.OutgoingSPA = $false;
		Set-MAPIAccount $_;
	}
}

# Restores the SMTP server to the original value on all non Exchange accounts.
# Useful to switch back to the standard SMTP settings after 
# a call to "ChangeOutlookAccountsSMTPServer".	
#
function RestoreOutlookAccountsSMTPServer()
{
	$a = Get-MAPIAccount "Outlook" | where-object {!$_.IsExchange}
	$b = Import-Clixml $home\OutlookAccounts.xml

	foreach($i in $a) {
		$b | % {
			if($_.AccountId -eq $i.AccountId) {
			
				$i.OutgoingServer = $_.OutgoingServer; 
				$i.OutgoingUseAuthentication = $_.OutgoingUseAuthentication;
				$i.OutgoingPort = $_.OutgoingPort; 
				$i.OutgoingSSL = $_.OutgoingSSL; 
				$i.OutgoingSPA = $_.OutgoingSPA; 
				Set-MAPIAccount $i;  
			}			
		}
	}	
}
